﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace CoolCode.Net
{
    public  class EmailSenderFactory
    {
        public static ISender ProvideGmail(string username, string password)
        {
            EmailSender sender = new EmailSender(username , password);
            sender.Server = "smtp.gmail.com";
            sender.EnableSsl = true;
            sender.Port = 587;
            return sender; 
        }

        public static ISender Provide21cn_com(string username, string password)
        {
            EmailSender sender = new EmailSender(username , password);
            sender.Server = "smtp-ent.21cn.com"; 
            return sender;             
        }

        public static ISender Provide126_com(string username, string password)
        {
            EmailSender sender = new EmailSender(username, password);
            sender.Server = "smtp.126.com"; 
            return sender;
        }

        /*
         *  配置文件例子：
         	<add key="mail-server" value="smtp.gmail.com"/>
		    <add key="mail-username" value="coolcode1@gmail.com"/>
		    <add key="mail-password" value="googlemail"/>
		    <!--如果不填，则默认端口25-->
		    <add key="mail-port" value="587"/>
		    <!--Ssl安全连接: 如果不填，则默认值是false-->
		    <add key="mail-enableSsl" value="true"/>
         */
        /// <summary>
        /// 从配置文件获取Email发送者
        /// <example>
        /// <para></para>
        /// </example>
        /// </summary>
        /// <returns></returns>
        public static ISender ProvideFromConfig()
        {
            string server = ConfigurationManager.AppSettings["mail-server"];
            string username = ConfigurationManager.AppSettings["mail-username"];
            string password = ConfigurationManager.AppSettings["mail-password"];
            int port = 25;
            int.TryParse(  ConfigurationManager.AppSettings["mail-port"], out port);
            bool enableSsl = false ;
            bool.TryParse( ConfigurationManager.AppSettings["mail-enableSsl"], out enableSsl);
            EmailSender sender = new EmailSender(username, password);
            sender.Server = server ;
            sender.Port = port;
            sender.EnableSsl = enableSsl ;
            return sender;
        }
    }
}
